﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using System.IO;

namespace Batman.Open.Accounts.Core
{
    public class DbContextFactory : IDesignTimeDbContextFactory<EntitiesDbContext>
    {
        EntitiesDbContext IDesignTimeDbContextFactory<EntitiesDbContext>.CreateDbContext(string[] args)
        {
            var jsonFile = "appsettings.json";
            // if (args != null && args.Length > 0 && args[0].Equals("production"))
            // {
            //     jsonFile = "appsettings.Production.json";
            // }

            IConfigurationRoot configuration = new ConfigurationBuilder()
               .SetBasePath(Directory.GetCurrentDirectory())
               .AddJsonFile(jsonFile)
               .Build();
            var builder = new DbContextOptionsBuilder<EntitiesDbContext>();
            var connectionString = configuration.GetConnectionString("DefaultConnection");
            builder.UseSqlServer(connectionString);

            return new EntitiesDbContext(builder.Options);
        }
    }
}
